clear all;
clc;

load mnist0v1;

trainsize = 100;

X = double(data(1:400,:)); %size n, both labeled and unlabeled data
Y = double(label(1:trainsize)); %size l < n, labels

l = size(Y,1);
n = size(X,1);

dim = 2;
sigma = 1;
c = 1*n;
C = 1;

K1 = X*X';
K2 = (1+K1).^dim;
xs = diag(K1);
K3 = exp((2*K1 - repmat(xs,1,n) - repmat(xs',n,1))/(2*sigma));

K1 = K1/max(max(K1));
K2 = K2/max(max(K2));
K3 = K3/max(max(K3));

cvx_begin sdp
    cvx_solver sedumi
    variable mu1
    variable mu2
    variable mu3
    variable delta(l,1) nonnegative
    variable nu(l,1) nonnegative
    variable t
    
    minimize t
    subject to
        K = mu1*K1 + mu2*K2 + mu3*K3
        trace(K) == c
        K >= 0
        Q = (Y*Y').*K(1:l,1:l)
        [Q (ones(l,1)+nu-delta); (ones(l,1)+nu-delta)' t-2*C*ones(1,l)*delta] >= 0
cvx_end

Xts = data(trainsize+1:size(label,1),:);

a = Q\(ones(l,1)+nu-delta);
Ypred = int8(sign(K(trainsize+1:n,1:trainsize)*(Y.*a)));

calacc(Ypred,label(trainsize+1:n))
